﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{

    CharacterController2D cc2d;
    Rigidbody2D rigid;
    Animator anim;

    public float speed = 5.0f;
    private float move;
    private bool jump;

    void Start()
    {
        cc2d = GetComponent<CharacterController2D>();
        rigid = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        move = Input.GetAxis("Horizontal");
        float temp = move;

        move *= speed;
        jump = Input.GetButton("Jump");

        //动画部分
        if (cc2d.isLanding)
        {
            anim.SetFloat("speed", Mathf.Abs(temp));
            anim.SetBool("jump_up", false);
            anim.SetBool("jump_down", false);
        }
        else {
            //2D刚体的速度
            Vector3 vel = rigid.velocity;
            //vel.y是刚体的纵向速度
            //如果纵向速度大于0，代表人物是往上的，纵向速度小于0，代表人物是往下的
            if (vel.y > 0)
            {
                anim.SetBool("jump_up", true);
                anim.SetBool("jump_down", false);
            }
            else {
                anim.SetBool("jump_up", false);
                anim.SetBool("jump_down", true);
            }
        }
    }

    private void FixedUpdate()
    {
        cc2d.Move(move, jump);
    }
}
